﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PhotoGallery.BLL;
using PhotoGallery.DLL;
using PhotoGallery.Model;
public partial class personal : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        User u = (User)Session["user"];
        if (u == null) Response.Redirect("login.aspx");
        else 
        {
            
            string uid = u.Uid;
            if (!IsPostBack)
            {
                this.username.Text = u.Nickname;
            }
            //判断是否有头像相册，如果有，则显示头像，如果没有则显示默认头像
            Picture avatorImg = getAvator(uid);
            if (avatorImg != null)
            {
                this.avator.ImageUrl = avatorImg.Pic;
                this.avator.Width = 50;
                this.avator.Height = 50;
            }
            else
            {
                this.avator.ImageUrl = "Images/user.png";
            }
        }
    }
    //判断是否有头像相册,如果有，则返回头像，如果没有，返回null
    private Picture getAvator(string uid) 
    {
        AlbumAction aa = new AlbumAction();
        PhotoAction pa = new PhotoAction();
        List<Albums> alist=aa.allAlbumsofSomeone(uid);//取得某用户的所有相册
        if (alist.Count == 0) return null;//无相册，返回空
        //有相册
        foreach (Albums a in alist) 
        {
            if (a.Title == "头像相册" && a.Desp == "头像相册")//有头像相册
            {
                return pa.showCover(a.Aid);
            }
            else //不是头像相册
            {
               
            }
        }
        return null;
    }
    //上传新头像
    protected void uploadAvt_Click(object sender, EventArgs e)
    {
        if (this.headimg.PostedFile != null)
        {
            string photoName1 = headimg.PostedFile.FileName; //获取初始文件名
            int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
            string newext = photoName1.Substring(i); //获取文件扩展名
            //判断文件是否为图片
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png"
                && newext != ".GIF" && newext != ".JPG" && newext != ".JPEG" && newext != ".BMP" && newext != ".PNG")
            {
                Response.Write("文件格式不正确!");
                Response.End();
            }
            string classid = ((User)Session["user"]).Uid;
            if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid))
            {
                System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid);
            }
            //修改文件名
            photoName1 = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-") + photoName1;
            headimg.PostedFile.SaveAs(Server.MapPath("photos\\" + classid + "\\" + photoName1));
            string path = "photos/" + classid + "/" + photoName1;
            Albums avatorAlbum = new Albums();
            //建立或者获取头像相册
            Picture old = getAvator(classid);
            if (old == null)
            {
                //之前没有，就新建一个头像相册
                AlbumAction aa = new AlbumAction();
                //获取新建的对象
                avatorAlbum=aa.newAlbum(classid, "头像相册", "头像相册");
            }
            else 
            {
                //取得相册对象
                AlbumAction aa = new AlbumAction();
                //获取头像相册
                avatorAlbum=aa.getAlbumbyCover(old.Id);
            }
            string coverid = picUpload(photoName1, avatorAlbum, classid, path);
            if (coverid != "0")
            {
                Response.Redirect("personal.aspx");
            }

        }
    }

    //图像存入数据库
    private string picUpload(string filename, Albums album, string uid, string path)
    {
        //图像存入数据库
        PhotoAction pa = new PhotoAction();
        pa.uploadPictures(uid, filename, "", album.Aid, path, "1");
        Picture p = pa.getPicbyUidndFileName(uid, filename);

        if (p != null)
        {
            //取得刚上传图片的id
            string id = p.Id;
            //修改封面
            AlbumAction aa = new AlbumAction();
            aa.modifyAlbum(album.Aid, id);
            return id;
        }
        else
        {
            return "0";
        }
    }
    protected void changeNickname_Click(object sender, EventArgs e)
    {
        string nickname = this.username.Text;
        UserAction pa = new UserAction();
        pa.changenickname(((User)Session["user"]).Uid, nickname);
    }
    protected void changePwd_Click(object sender, EventArgs e)
    {
        string password = Get_MD5(this.userpasswd.Text);
        UserAction pa = new UserAction();
        pa.changePwd(((User)Session["user"]).Uid, password);
        Response.Redirect("login.aspx");
    }
    public string Get_MD5(string strSource)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5");
    }
}